# !/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName : zsj.py
# @Time     : 2024/4/18 21:17
# @Author   : Robot-Zsj
"""
description:

i:
AAAAHHHBBCDHHHH
3

o:
2
"""
import pdb

s = input()
k = int(input())


def getResult():
    global s
    global k

    if k <= 0:
        return -1

    s += "0"
    count = {}

    b = s[0]
    long = 1

    for i in range(1, len(s)):
        c = s[i]

        if b == c:
            long += 1
        else:
            if count.get(b) is None or count[b] < long:
                count[b] = long
            long = 1
            b = c

    arr = list(count.values())

    if k > len(arr):
        return -1
    else:
        arr.sort(reverse=True)
        return arr[k - 1]


print(getResult())
